home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-05-21 | 9.6 KB | 269 lines | [TEXT/ttxt] |
- in module AnalogClockModule
-
- -- Note: 0 and (2 * pi) degrees radian is at 3 o'clock and increases clockwise
- global constant angleAt12 := (1.5 * pi)
- global constant anglePerSec := (pi / 30)
-
-
- class ClockHands (RotatingShape)
- end
-
- --*******************************************************************************
- --* Class name: AnalogClock
- --*
- --* Inherits from: TwoDMultiPresenter
- --* Class type: Concrete
- --* Component: Spaces and Presenters
- --*
- --* Description: This is a subclass of TwoDMultiPresenter which consists of
- --* 3 clock hands: the hour hand, the minute hand and the second
- --* hand. To start the clock, call the startClock method. It
- --* sets the time then starts a callback which updates the clock
- --* every second. Only the second hand gets updated every second.
- --* The minute and hour hands get updated on the minute.
- --*
- --* Usage: ac := new AnalogClock radius:60 \
- --* clockFace:<some bitmap> \
- --* hourHand:<some bitmap> \
- --* minHand:<some bitmap> \
- --* hourAxis:(new Point x:0 y:0) \
- --* minAxis:(new Point x:0 y:0)
- --*
- --* IVs: radius
- --* hourHand
- --* minuteHand
- --* secondHand
- --*
- --* Methods: makeClockHand
- --* setHours
- --* setMinutes
- --* setSeconds
- --* setTime
- --* updateClock
- --* startClock
- --* init
- --* afterInit
- --*
- --* Required files: rot8shp.sx
- --*
- --* Notes:
- --*
- --* Author: Su Quek - Kaleida Labs, Inc.
- --*******************************************************************************
- class AnalogClock (TwoDMultiPresenter)
- inst vars
- radius
- hourHand
- minuteHand
- secondHand
- end
-
- --*=============================================================================*
- --* Method name: makeClockHand
- --* Class: AnalogClock
- --* Usage: makeClockHand self target
- --* target - Bitmap
- --*-----------------------------------------------------------------------------*
- --* Description: Creates a ClockHand.
- --*=============================================================================*
- method makeClockHand self {class AnalogClock} target axis ->
- (
- local clockRadius := self.radius
- local theHand := new ClockHands target:target axisPos:axis
- theHand.fill := blackBrush
- theHand.position := new Point x:(clockRadius - axis.x) y:(clockRadius - axis.y)
- prepend self theHand
-
- return theHand
- )
-
- --*=============================================================================*
- --* Method name: makeSecondHand
- --* Class: AnalogClock
- --* Usage: makeSecondHand self
- --*-----------------------------------------------------------------------------*
- --* Description: Creates a Line.
- --*=============================================================================*
- method makeSecondHand self {class AnalogClock} ->
- (
- local clockRadius := self.radius
- local theLine := (new Line x2:0 y2:(0.8 * clockRadius) )
- local secondHand := new TwoDShape boundary:theLine \
- stroke:(new Brush color:redColor)
- secondHand.position := new Point x:clockRadius y:clockRadius
- prepend self secondHand
-
- self.secondHand := secondHand.boundary
-
- return secondHand.boundary
- )
-
- --*=============================================================================*
- --* Method name: setHours
- --* Class: AnalogClock
- --* Usage: setHours self
- --*-----------------------------------------------------------------------------*
- --* Description: Rotates the hour hand clockwise by 0.5 degrees.
- --* # degrees/minute = (30 degrees) / (60 minutes) = 0.5
- --*=============================================================================*
- method setHours self {class AnalogClock} ->
- (
- rotate self.hourHand 0.5 @degrees
- )
-
- --*=============================================================================*
- --* Method name: setMinutes
- --* Class: AnalogClock
- --* Usage: setMinutes self
- --*-----------------------------------------------------------------------------*
- --* Description: Rotates the minute hand clockwise by 6 degrees.
- --* # degrees/minute = (360 degrees) / (60 minutes) = 6
- --*=============================================================================*
- method setMinutes self {class AnalogClock} ->
- (
- rotate self.minuteHand 6 @degrees
- )
-
- --*=============================================================================*
- --* Method name: setSeconds
- --* Class: AnalogClock
- --* Usage: setSeconds self
- --*-----------------------------------------------------------------------------*
- --* Description: Rotates the second hand by the angle moved per second.
- --* (angle @ 12 o'clock) + ((# second) * (2 * pi) / (60 seconds))
- --*
- --* angleAt12 = (1.5 * pi); anglePerSec = (pi / 30)
- --*=============================================================================*
- method setSeconds self {class AnalogClock} ->
- (
- local theSec := theCalendarClock.date.seconds
- self.secondHand.angle := angleAt12 + (theSec * anglePerSec)
- notifyChanged self true
-
- return theSec
- )
-
- --*=============================================================================*
- --* Method name: setTime
- --* Class: AnalogClock
- --* Usage: setTime self
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the hour, minute and second hands of the clock.
- --* Hour: (# hour) * (360 degrees) / (12 hours)
- --* (# minute) * (30 degrees) / (60 minutes)
- --* Minute: (# minute) * (360 degrees) / (60 minutes)
- --* Second: (angle @ 12 o'clock) +
- --* ((# second) * (2 * pi) / (60 seconds))
- --*
- --* angleAt12 = (1.5 * pi); anglePerSec = (pi / 30)
- --*=============================================================================*
- method setTime self {class AnalogClock} ->
- (
- local theDate := theCalendarClock.date
- local theHour := theDate.hours
- local theMin := theDate.minutes
- local theSec := theDate.seconds
-
- --*=========================================================================*
- --* Set the second hand
- --*=========================================================================*
- self.secondHand.angle := angleAt12 + (theSec * anglePerSec)
-
- --*=========================================================================*
- --* Set the minute hand
- --*=========================================================================*
- rotate self.minuteHand (theMin * 360 / 60) @degrees
-
- --*=========================================================================*
- --* Set the hour hand
- --*=========================================================================*
- rotate self.hourHand (theHour * 360 / 12) @degrees
- rotate self.hourHand (theMin * 30 / 60) @degrees
-
- return
- )
-
- --*=============================================================================*
- --* Method name: updateClock
- --* Class: AnalogClock
- --* Usage: updateClock self
- --*-----------------------------------------------------------------------------*
- --* Description: Updates the hour, minute and second hands of the clock.
- --* The minute and hour hands get updated on the minute.
- --*=============================================================================*
- method updateClock self {class AnalogClock} ->
- (
- if ((setSeconds self) = 0) do
- (
- setMinutes self
- setHours self
- )
-
- return
- )
-
- --*=============================================================================*
- --* Method name: startClock
- --* Class: AnalogClock
- --* Usage: startClock self
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the time then starts a callback which updates the clock
- --* every second.
- --*=============================================================================*
- method startClock self {class AnalogClock} ->
- (
- setTime self
- addPeriodicCallback theCalendarClock updateClock self #() 1
- )
-
-
- --*=============================================================================*
- --* Method name: init
- --* Class: AnalogClock
- --* Usage: init self [radius:<Number>] \
- --* [clockFace:<Bitmap>] \
- --* [hourHand:<Bitmap>] \
- --* [hourAxis:<Point>] \
- --* [minHand:<Bitmap>] \
- --* [minAxis:<Point>]
- --*-----------------------------------------------------------------------------*
- --* Description: Creates and initializes the TwoDMultiPresenter.
- --*=============================================================================*
- method init self {class AnalogClock} #rest args \
- #key radius:(75) \
- clockFace:(new Bitmap) \
- hourHand:(new Bitmap) \
- hourAxis:(new Point x:0 y:0) \
- minHand:(new Bitmap) \
- minAxis:(new Point x:0 y:0) ->
- (
- apply nextmethod self boundary:clockFace.bbox args
- return self
- )
-
- --*=============================================================================*
- --* Method name: afterInit
- --* Class: AnalogClock
- --* Usage: afterInit self [radius:<Number>]
- --*-----------------------------------------------------------------------------*
- --* Description: Sets the radius IV and creates the hands of the clock.
- --*=============================================================================*
- method afterinit self {class AnalogClock} #rest args \
- #key radius:(75) \
- clockFace:(new Bitmap) \
- hourHand:(new Bitmap) \
- hourAxis:(new Point x:0 y:0) \
- minHand:(new Bitmap) \
- minAxis:(new Point x:0 y:0) ->
- (
- self.radius := radius
-
- append self (new TwoDShape boundary:clockFace fill:defaultBrush)
- self.hourHand := (makeClockHand self hourHand hourAxis)
- self.minuteHand := (makeClockHand self minHand minAxis)
- self.secondHand := (makeSecondHand self)
-
- return self
- )
- "Loaded anlogclk.sx"
-